<!--
  // XX仓库>待入库采购单>入库记录(采购单中物料的发货\入库情况详情页)
  showPurchaseOrderMaterialsDetailByDeliverAndInStoreOrder: 'showPurchaseOrderMaterialsDetailByDeliverAndInStoreOrder',

这个页面有两个ViewState：主要供应商版、普通供应商版
-->
<template>
	<a-modal title="采购单入库记录" :width="1200" :visible="visible" :confirmLoading="confirmLoading" :footer="null"
		@ok="handleSubmit" @cancel="handleCancel">
		<a-card :bordered="false">
			<div class="table-page-search-wrapper">
				<a-form layout="inline">
					<a-row :gutter="48">

						<a-col :md="8" :sm="24" v-if="false">
							<a-form-item label="订单ID">
								<a-input v-model="queryParam.orderId" placeholder="请输入订单ID" />
							</a-form-item>
						</a-col>
					</a-row>
				</a-form>
			</div>

			<s-table ref="table" size="default" :columns="columnsDynamic" :data="loadData" :alert="true"
				:rowKey="(record) => record.code"
				:rowSelection="null && { selectedRowKeys: selectedRowKeys, onChange: onSelectChange }" :showPagination="false">
				<template slot="extend1" slot-scope="text, record">
					{{ record.material ? record.material.extend1 : '' }}
				</template>
				<template slot="extend3" slot-scope="text, record">
					{{ record.material ? record.material.extend3 : '' }}
				</template>
				<template slot="materialModelNumber" slot-scope="text, record">
					{{ record.material ? record.material.materialModelNumber : '' }}
				</template>
				<!-- <span slot="image" slot-scope="text">
					<image-preview :images="text" />
				</span> -->
				<span slot="image" slot-scope="text,record">
					<image-preview1 :images="text" :imageUrl="record.imageUrl" />
				</span>
				<template slot="thisCompleteNumber" slot-scope="text, record">
					<a-input-number v-model:value="record.thisCompleteNumber" :step="1" :min="0" style="width: 67px;" />
				</template>

				<span slot="actionCommon" slot-scope="text, record" v-if="extra.title != '采购单入库记录'">
					<!--    查进度明细时，需要筛选出源引过该记录的订单      -->
					<a v-if="extra.title != '入库记录'" @click="$refs.inStoreSourceForm.show(Object.assign({ sourceOrderMaterialIds: record.orderMaterialId }, record),
						'订单列表', 'common')">入库明细</a>

					<!--    UI上显示的是订单物料列表,而不是订单.因此还要循环用一下本页面      -->
					<a v-else @click="$refs.inStoreSourceForm.show(
						{
							sourceOrderMaterialId: record.orderMaterialId,
							orderType: $businessEnum.ORDER_TYPE_ENUM.inOrder.id
						},
						{ title: '采购单入库记录', viewState: 'orderMaterialCommon' })">入库明细</a>
				</span>

			</s-table>

			<!--  采购单物料入库流水页  -->
			<in-store-source-form ref="inStoreSourceForm" @ok="handleOk" />

		</a-card>
	</a-modal>
</template>

<script>
import Vue from 'vue'
import { ACCESS_TOKEN } from '@/store/mutation-types'
import { STable } from '@/components'
import { page, del, add } from '@/api/modular/main/emsordermaterial/emsOrderMaterialManage'
import inStoreSourceForm from '@/views/main/emsordermaterial/purchaseOrder/emsInStoreSourceForm'

export default {
	components: {
		STable,
		inStoreSourceForm,
	},

	data() {
		return {
			title: null,
			advanced: false, // 高级搜索 展开/关闭
			queryParam: {},  // 查询参数
			// 加载数据方法 必须为 Promise 对象
			loadData: parameter => {
				return page(Object.assign(parameter, this.queryParam)).then((res) => {
					res.data.rows.forEach(value => {
						value.thisCompleteNumber = 0
					})
					return res.data
				})
			},
			selectedRowKeys: [],
			selectedRows: [],
			defaultExpandedKeys: [],
			visible: false,
			confirmLoading: false,
			emsOrder: null,
			viewStateEnum: {
				normalSupplier: 'normalSupplier',
				primarySupplier: 'primarySupplier',
				purchaseOrderEnd: 'purchaseOrderEnd', //这个应该没有，UI和normalSupplier长的一样
			},
			viewState: '',
			extra: {},
		}
	},
	computed: {
		token() {
			return Vue.ls.get(ACCESS_TOKEN)
		},
		columnsDynamic() {
			let normalSupplierColumns = [
				{
					title: '序号', width: '80px',
					customRender: (text, record, index) => `${index + 1}`
				},
				...this.columnsMaterialProperty,
				{
					title: '总数量',
					dataIndex: 'number'
				},
				// {
				//   title: '入库数量',
				//   dataIndex: 'completedNumber',
				//   customRender: (text, record, index) => '没算'
				// },
				{
					title: '状态',
					dataIndex: 'executeStatus',
					customRender: (text, record, index) => {
						let es = this.$util.findExecuteStatusById(text)
						return es ? es.name : '未知状态'
					}
				},
			]
			let primarySupplierColumns = [
				// {
				//   title: '发货待入库数量',
				//   dataIndex: '',
				//   customRender: (text, record, index) => '没算'
				// },
				// {
				//   title: '异常数量',
				//   dataIndex: '',
				//   customRender: (text, record, index) => '没算'
				// },
				{
					title: '操作',
					width: '150px',
					scopedSlots: { customRender: 'actionCommon' }
				}
			]
			let columns
			if (this.viewState == this.viewStateEnum.normalSupplier) {
				columns = [...normalSupplierColumns]
			} else if (this.viewState == this.viewStateEnum.primarySupplier) {
				columns = [...normalSupplierColumns, ...primarySupplierColumns]
			} else if (this.viewState == this.viewStateEnum.purchaseOrderEnd) {
				columns = [...normalSupplierColumns,
				{
					title: '操作',
					width: '150px',
					scopedSlots: { customRender: 'actionCommon' }
				}
				]
			}
			return columns
		},
		columnsMaterialProperty() {
			let result = [
				{
					title: '名称',
					dataIndex: 'materialName'
				},
				{
					title: '订单号',
					dataIndex: 'orderNumber',
					ellipsis: true
				},
				{
					title: '订单类型',
					dataIndex: 'orderType',
					customRender: (text, record, index) => {
						let otEnum = this.$util.findOrderTypeById(text)
						return otEnum ? otEnum.name : '未知类型'
					}
				},
				{
					title: '料号',
					dataIndex: 'materialCode'
				},
				{
					title: '规格',
					dataIndex: 'materialModelNumber',
					scopedSlots: { customRender: 'materialModelNumber' }
				},
				{
					title: '图片',
					dataIndex: 'image',
					scopedSlots: { customRender: 'image' }
				},
				{
					title: '需求数量',
					dataIndex: 'number'
				},
				{
					title: '已入库数量',
					dataIndex: 'completedNumber'
				},
				{
					title: '状态',
					dataIndex: 'executeStatus',
					customRender: (text, record, index) => {
						let es = this.$util.findExecuteStatusById(text)
						return es ? es.name : '未知状态'
					}
				},
				{
					title: '备注',
					dataIndex: 'remark'
				},
			]

			if (!this.extra) {
				return result
			}

			if (this.extra.materialType == this.$businessEnum.MATERIAL_TYPE_ENUM.material.id) {
				result = [
					{
						title: '原料编号',
						dataIndex: 'materialCode'
					},
					{
						title: '原料名称',
						dataIndex: 'materialName'
					},
					{
						title: '规格',
						dataIndex: 'materialModelNumber',
						scopedSlots: { customRender: 'materialModelNumber' }
					},
					{
						title: '图片',
						dataIndex: 'image',
						scopedSlots: { customRender: 'image' }
					},
				]
			} else if (this.extra.materialType == this.$businessEnum.MATERIAL_TYPE_ENUM.product.id) {
				result = [
					{
						title: '产品编号',
						dataIndex: 'materialCode'
					},
					{
						title: '产品名称',
						dataIndex: 'materialName'
					},
				]
			} else if (this.extra.materialType == this.$businessEnum.MATERIAL_TYPE_ENUM.packing.id) {
				result = [
					{
						title: '包材编号',
						dataIndex: 'materialCode'
					},
					{
						title: '包材名称',
						dataIndex: 'materialName'
					},
					{
						title: '规格',
						dataIndex: 'materialModelNumber',
						scopedSlots: { customRender: 'materialModelNumber' }
					},
					{
						title: '尺寸',
						dataIndex: 'extend1',
						scopedSlots: { customRender: 'extend1' }

					},
					{
						title: '图片',
						dataIndex: 'image',
						scopedSlots: { customRender: 'image' }
					},
				]
			} else if (this.extra.materialType == this.$businessEnum.MATERIAL_TYPE_ENUM.cable.id) {
				result = [
					{
						title: '线缆编号',
						dataIndex: 'materialCode'
					},
					{
						title: '线缆名称',
						dataIndex: 'materialName'
					},
					{
						title: '颜色',
						dataIndex: 'extend3',
						scopedSlots: { customRender: 'extend3' }

					},
					{
						title: '规格',
						dataIndex: 'materialModelNumber',
						scopedSlots: { customRender: 'materialModelNumber' }
					},
					{
						title: '图片',
						dataIndex: 'image',
						scopedSlots: { customRender: 'image' }
					},
				]
			}
			return result
		}
	},
	methods: {
		show(record, extra) {
			this.visible = true
			this.emsOrder = record
			this.extra = extra

			this.queryParam.pageSize = -1
			this.queryParam.orderId = record.orderId
			this.queryParam.materialType = extra.materialType

			if (record.executeStatus == this.$businessEnum.EXECUTE_STATUS_ENUM.COMPLETED.id) {
				// XX仓库>已入库采购单>入库详情(采购单中物料的入库情况详情页)
				// showPurchaseOrderMaterialsDetailByInStoreOrder: 'showPurchaseOrderMaterialsDetailByInStoreOrder',
				this.viewState = this.viewStateEnum.purchaseOrderEnd
			} else if (record.targetOrgType == this.$businessEnum.ORG_TYPE_ENUM.NORMAL_SUPPLIER.id) {
				this.viewState = this.viewStateEnum.normalSupplier
			} else if (record.targetOrgType == this.$businessEnum.ORG_TYPE_ENUM.PRIMARY_SUPPLIER.id) {
				this.viewState = this.viewStateEnum.primarySupplier
			} else {
				this.viewState = ''
			}
			setTimeout(() => {
				this.handleOk()
				this.$refs.table.clearSelected()
			}, 100)
		},
		handleOk() {
			this.$refs.table.refresh()
		},
		onSelectChange(selectedRowKeys, selectedRows) {
			this.selectedRowKeys = selectedRowKeys
			this.selectedRows = selectedRows
		},
		handleSubmit() {
		},
		handleCancel() {
			this.visible = false
		},
		onEmsMaterialIndex1Selected(materialRow) {
			// console.log(`选择了原料:id=${materialRow.materialId}, code=${materialRow.materialCode}, name=${materialRow.materialName}`)
			// debugger
			let { materialId, materialName, materialCode, materialType, costPrice } = materialRow
			// 需要调用接口往订单中添加物料
			let _emsOrder = Object.assign({}, this.emsOrder)
			_emsOrder.orderMaterials = []
			let orderMaterial = {
				orderMaterialId: '',
				orderId: _emsOrder.orderId,
				bomId: '0',  // 单独选择原料时没有BOMID
				materialId: materialId,
				number: 1,
				completedNumber: 0,
				executeStatus: '0',
				executeProgress: '0',
				status: '0',
				createTime: '',
				createUser: '',
				updateTime: '',
				updateUser: '',
			}
			_emsOrder.orderMaterials.push(orderMaterial)
			add(orderMaterial).then(res => {
				if (res.success) {
					this.$message.success('新增成功')
					// this.handleCancel()
					// this.$emit('ok', values)
					this.$refs.table.refresh()
				} else {
					this.$message.info('新增失败：' + res.message)
				}
			})
			// 然后刷新table控件
		},
		imageURLFormat(text) {
			return `${this.$store.state.baseUrl}/sysFileInfo/preview?id=${text}&token=${this.token}`
		}
	}
}
</script>

<style lang="less">
.table-operator {
	margin-bottom: 18px;
}

button {
	margin-right: 8px;
}
</style>
